$(document)
		.ready(
				function() {
					setValidate($("#roleForm"), {
						rules : {
							roleName : {
								required : true,
								remote : {
									type : "POST",
									url : _BMSROLE_IS_EXIST_URL,
									data : {
										roleName : function() {
											return $("#roleName").val();
										},
										roleId : _ROLE_ID
									}
								}
							}
						},
						messages : {
							roleName : {
								required : "角色名称不能为空",
								remote : "角色已存在"
							}
						}
					});

					var rpgs = _ROLE_PERMISSIONGROUPS;
					$("#pg_tree input[type='checkbox'][name='pmsg_id']")
							.parent().addClass('uncheckedLabel');
					for ( var x in rpgs) {
						var $e = $("#pg_tree input[type='checkbox'][name='pmsg_id'][value='"
								+ rpgs[x].permissionGroupId + "']");
						$e.prop('checked', true);
						// 选中父
						checkParent($e);
						// 选中子
						// checkChild($e , true);
						setLabelCss($e, true);
					}

					$("#pg_tree input[type='checkbox']").bind('click',
							function(event) {
								event.stopPropagation();
								var $this = $(this);
								// 选中子
								var isChecked = $this.is(':checked');
								checkChild($this, isChecked);

								// 选中父
								checkParent($this);
							});

					$("#pg_tree").treeview();
				});

function checkParent(obj) {
	var $obj = $(obj);
	var $parent = $("#pg_tree input[type='checkbox'][id='"
			+ $obj.attr('parent_id') + "']");
	if ($parent.length != 0) {
		var checked = 0;
		$parent.parent().parent().find("input[name='pmsg_id']").each(
				function(index, e) {
					if ($(e).is(':checked')) {
						checked = checked + 1;
					}
				});
		if (checked == 0) {
			$parent.prop('checked', false);
		} else {
			$parent.prop('checked', true);
		}
		checkParent($parent)
	}
}

function checkChild(obj, isChecked) {
	var $obj = $(obj);
	var $children = $("#pg_tree input[type='checkbox'][parent_id='"
			+ $obj.attr('id') + "']");
	if ($children.length != 0) {
		$children.each(function(index, child) {
			$child = $(child);
			$child.prop('checked', isChecked);
			checkChild($child, isChecked);
		});
	}
	setLabelCss($obj, isChecked);
}

function setLabelCss($obj, isChecked) {
	if ($obj.parent().is("label")) {
		if (isChecked) {
			$obj.parent().removeClass("uncheckedLabel");
			$obj.parent().addClass("checkedLabel");
		} else {
			$obj.parent().removeClass("checkedLabel");
			$obj.parent().addClass("uncheckedLabel");
		}
	}
}

function saveRole() {
	if ($("input[name='pmsg_id']:checked").length <= 0) {
		_alert("请选择关联权限", false);
		return;
	}

	bmsAjaxForm($("#roleForm"), {
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			if (data.code == 1) {
				_alert("保存失败," + data.message, false);
			} else {
				_alert('保存成功', true);
				$.go(_BMSROLE_LIST_URL, {
					dataType : 'html',
					success : function(data, textStatus) {
						replaceDocument(_BMSROLE_LIST_URL, data);
					}
				});
			}
		}
	});
}